package com.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 系统配置管理类
 *
 * @author: dingjiejun
 * @version: 1.0
 * @date: 2017/3/23
 */
@Component
public class ConfigHolder {

    private static Logger logger = LoggerFactory.getLogger(ConfigHolder.class);

    private static Map<String, String> propertiesMap = new HashMap<String, String>();

    /**
     * 是否系统调试模式
     */
    private static Boolean debug = false;

    public static boolean isDebug() {
        return debug;
    }
    @Value("${sys.mode.debug:false}")
    public void setDebug(Boolean debug) {
        ConfigHolder.debug = debug;
    }

    /**
     * 数据库字段到结果集访问键的映射方式 0 驼峰法(默认)  1 小写
     */
    private static int dbFiledMapMethod = 0;

    public static int getDbFiledMapMethod() {
        return dbFiledMapMethod;
    }

    @Value("${sys.db.field.map.method:0}")
    public void setDbFiledMapMethod(int dbFiledMapMethod) {
        ConfigHolder.dbFiledMapMethod = dbFiledMapMethod;
    }

    /**
     * excel模板路径
     */
    private static String excelTemplatePath;

    public static String getExcelTemplatePath() {
        return excelTemplatePath;
    }

    @Value("${sys.excel.template.path:/excel/}")
    public void setExcelTemplatePath(String excelTemplatePath) {
        ConfigHolder.excelTemplatePath = excelTemplatePath;
    }





    /**
     * 获取参数值
     * @param key
     * @return
     */
    public static String get(String key) {
        return propertiesMap.get(key);
    }

    /**
     * 获取参数值，没找到则使用默认值
     * @param key
     * @param defaultValue
     * @return
     */
    public static String get(String key , String defaultValue) {
        String value = propertiesMap.get(key);
        return value == null ? defaultValue:value;
    }

    public static Boolean getBoolean(String key) {
        String value = get(key);
        if (value == null) {
            return false;
        }
        return Boolean.valueOf(value);
    }

    public static Integer getInt(String key) {
        String value = get(key);
        if (value == null) {
            return 0;
        }
        return Integer.valueOf(value);
    }

    public static Long getLong(String key) {
        String value = get(key);
        if (value == null) {
            return 0L;
        }
        return Long.valueOf(value);
    }

    public static Double getDouble(String key) {
        String value = get(key);
        if (value == null) {
            return 0d;
        }
        return Double.valueOf(value);
    }

    public static Float getFloat(String key) {
        String value = get(key);
        if (value == null) {
            return 0f;
        }
        return Float.valueOf(value);
    }

    public static void initProp(Properties properties) {
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            String key = (String) entry.getKey();
            String value = String.valueOf(entry.getValue());
            propertiesMap.put(key, value);
            if (logger.isDebugEnabled()) {
                logger.debug("{}:{}", key, value);
            }
        }
    }

}
